package cn.innosmart.aq.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.tencent.android.tpush.XGPushConfig;
import com.tutk.p2p.ConnectionEntity;
import com.tutk.p2p.ConnectionManager;

import cn.innosmart.aq.R;
import cn.innosmart.aq.isp.ISPTool;
import cn.innosmart.aq.util.SharedUtil;
import cn.innosmart.aq.util.SystemConstant;
import cn.innosmart.aq.view.activity.base.BaseActivity;

/**
 * Created by David on 2015/9/2.
 */
public class PushMessageSettingAcitivity extends BaseActivity {
    private final int EXIT = 0;
    private final int SUBSCRIBEERROR_XG = -1;
    private final int UNSUBSCRIBEERROR_XG = -2;
    private final int SUBSCRIBEERROR_WX = -3;
    private final int UNSUBSCRIBEERROR_WX = -4;
    private ToggleButton tbXGSwitchButton;
    private ToggleButton tbWXSwitchButton;
    private ConnectionEntity connectionEntity;
    private RelativeLayout rlTel;
    private RelativeLayout rlSms;
    /**
     * 设置界面Toolbar
     */
    private Toolbar toolbar;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EXIT:
                    exit();
                    break;
                case SUBSCRIBEERROR_XG:
                    showToast(getString(R.string.subscribe_error));
                    tbXGSwitchButton.setChecked(false);
                    break;
                case UNSUBSCRIBEERROR_XG:
                    showToast(getString(R.string.unsubscribe_error));
                    tbXGSwitchButton.setChecked(true);
                    break;
                case SUBSCRIBEERROR_WX:
                    showToast(getString(R.string.subscribe_error));
                    tbXGSwitchButton.setChecked(false);
                    break;
                case UNSUBSCRIBEERROR_WX:
                    showToast(getString(R.string.unsubscribe_error));
                    tbXGSwitchButton.setChecked(true);
                    break;

            }
        }
    };
    private View.OnClickListener mBtnOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String xingeToken = XGPushConfig.getToken(getApplicationContext());
            if (xingeToken == null) {
                showToast(getString(R.string.activity_push_error));
                return;
            }
            switch (v.getId()) {
                case R.id.tb_switch_xinge:
                    if (tbXGSwitchButton.isChecked()) {
                        ISPTool.getInstance().updateISP("xinge", connectionEntity.getUid(), "yes", "", xingeToken, new ISPTool.onISPCallBack() {
                            @Override
                            public void onSuccess(int resultcode, int xingeId) {
                                SharedUtil.getInstance().writeXinStatus(true, connectionEntity.getUid());
                                showToast(getString(R.string.subscribe_success));
                            }

                            @Override
                            public void onFailed(int resultcode, int xingeId) {
                                mHandler.sendEmptyMessage(SUBSCRIBEERROR_XG);
                            }
                        });
                    } else {
                        ISPTool.getInstance().updateISP("xinge", connectionEntity.getUid(), "no", "", xingeToken, new ISPTool.onISPCallBack() {
                            @Override
                            public void onSuccess(int resultcode, int xingeId) {
                                SharedUtil.getInstance().writeXinStatus(false, connectionEntity.getUid());
                                SharedUtil.getInstance().writeDefaultSubscription(true, connectionEntity.getUid());
                                showToast(getString(R.string.unsubscribe_success));
                            }

                            @Override
                            public void onFailed(int resultcode, int xingeId) {
                                mHandler.sendEmptyMessage(UNSUBSCRIBEERROR_XG);
                            }
                        });
                    }
                    break;
                case R.id.tb_switch_wechat: {
                    process_toggle_wechat();
                }
                break;
                case R.id.rl_sms:
                    Intent intentSms = new Intent(PushMessageSettingAcitivity.this, SmsPushMessageActivity.class);
                    startActivity(intentSms);
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                    break;
                case R.id.rl_tel:
                    Intent intent = new Intent(PushMessageSettingAcitivity.this, TelPushMessageActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                    break;
            }
        }
    };

    private void process_toggle_wechat() {
        System.out.println("wx openid : " + SystemConstant.openid);
        if (SystemConstant.openid.trim().isEmpty()) {
            tbWXSwitchButton.setChecked(!tbWXSwitchButton.isChecked());
            Toast.makeText(getApplicationContext(), R.string.wechat_bind_please, Toast.LENGTH_LONG).show();
            return;
        }

        final String openid = SystemConstant.openid;

        if (tbWXSwitchButton.isChecked()) {
            ISPTool.getInstance().updateISP("wechat", connectionEntity.getUid(), "yes", "", openid, new ISPTool.onISPCallBack() {
                @Override
                public void onSuccess(int resultcode, int xingeId) {
                    SharedUtil.getInstance().writeWXStatus(true, connectionEntity.getUid());
                    showToast(getString(R.string.subscribe_success));
                }

                @Override
                public void onFailed(int resultcode, int xingeId) {
                    mHandler.sendEmptyMessage(SUBSCRIBEERROR_WX);
                }
            });
        } else {
            ISPTool.getInstance().updateISP("wechat", connectionEntity.getUid(), "no", "", openid, new ISPTool.onISPCallBack() {
                @Override
                public void onSuccess(int resultcode, int retId) {
                    SharedUtil.getInstance().writeWXStatus(false, connectionEntity.getUid());
                    SharedUtil.getInstance().writeDefaultSubscription(true, connectionEntity.getUid());
                    showToast(getString(R.string.unsubscribe_success));
                }

                @Override
                public void onFailed(int resultcode, int retId) {
                    mHandler.sendEmptyMessage(UNSUBSCRIBEERROR_WX);
                }
            });
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_push_message_setting);
        connectionEntity = ConnectionManager.getInstance().getCurrentConnectionEntity();
        tbXGSwitchButton = (ToggleButton) findViewById(R.id.tb_switch_xinge);
        tbWXSwitchButton = (ToggleButton) findViewById(R.id.tb_switch_wechat);

        rlTel = (RelativeLayout) findViewById(R.id.rl_tel);
        rlSms = (RelativeLayout) findViewById(R.id.rl_sms);
        if (SharedUtil.getInstance().readXinStatus(connectionEntity.getUid())) {
            tbXGSwitchButton.setChecked(true);
        } else {
            tbXGSwitchButton.setChecked(false);
        }

        if (SharedUtil.getInstance().readWXStatus(connectionEntity.getUid())) {
            tbWXSwitchButton.setChecked(true);
        } else {
            tbWXSwitchButton.setChecked(false);
        }

        tbXGSwitchButton.setOnClickListener(mBtnOnClickListener);
        tbWXSwitchButton.setOnClickListener(mBtnOnClickListener);

        rlTel.setOnClickListener(mBtnOnClickListener);
        rlSms.setOnClickListener(mBtnOnClickListener);

        setBar();
    }

    private void setBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ((ImageView) findViewById(R.id.iv_headicon)).setImageResource(R.drawable.toolbar_back);
        toolbar.setTitle("");
        ((TextView) findViewById(R.id.tv_toolbar)).setText(getString(R.string.push_message_setting));
        setSupportActionBar(toolbar);
        findViewById(R.id.iv_headicon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mDrawer.openDrawer(GravityCompat.START);
                mHandler.sendEmptyMessage(EXIT);
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onResume() {
        setBaseDialogContext(this);
        super.onResume();
        toolbar.setNavigationIcon(null);
    }


}
